package EGS.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.stationClient;

public class EGS_execute_service10003Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 10003 设备验证
	 */
	public EGS_execute_service10003Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if (msgClass.equals("10003")) {// 设备验证
				String eqName = "";
				try {
					eqName = Mroot.element("body").element("equipment").attribute("name").getValue();
				} catch (Exception e) {
					eqName = iStation;
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleM = eleB.addElement("maintain");// <maintain state="1" nextTime="2017-08-31 14:00:00"
															// alert="3">
				try {
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					eleM.addAttribute("state", "1");
					eleM.addAttribute("nextTime", GetTimestamp("yyyy-MM-dd hh:mm:ss"));
					eleM.addAttribute("alert", "0");
					AKSLogs(100031, iStation + "_EQ=" + eqName);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					eleM.addAttribute("state", "1");
					eleM.addAttribute("nextTime", GetTimestamp("yyyy-MM-dd hh:mm:ss"));
					eleM.addAttribute("alert", "0");
					AKSLogs(100030, iStation + "_EQ=" + eqName + ":_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

	private static String GetTimestamp(String iFormat) {
		// "2017-12-10 15:52:10";
		if(iFormat == null || iFormat.length()<1 ) {
			iFormat="yyyy-MM-dd HH:mm:ss SSS";
		}
		String timestamp = new String();
		SimpleDateFormat t = new SimpleDateFormat(iFormat);
		timestamp = "" + t.format(new Date());
		return timestamp;
	}
}
